package com.globagames.isometric.core.model
{
	
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Profile
	{		
		public var code:String;
		public var name:String;
		public var rects:Array;
		public var rectsGhost:Array;
		
		public var standing:Array = new Array();
		public var currentPose:int;
		
		public var animatePoses:Array = new Array();
		
		public var bitmapData:BitmapData;
		public var bitmapDataGhost:BitmapData;
		
		public function Profile()
		{
			//start();
		}
		/*
		public function start():void {
			var myTimer:Timer = new Timer(400, 0);
            myTimer.addEventListener("timer", timerHandler);
            myTimer.start();
  		}
		public function timerHandler(event:TimerEvent):void {
            //trace("timerHandler: character " + code);  
            if( animatePoses.length > 0 ) {
            	currentPose = animatePoses.shift();
            	if( isomap ) isomap.invalidateDisplayList(); 
            	
            }          
        }*/
		
		
		public function addBitmapToCharacter(_bitmapData:BitmapData, isGhost:Boolean ) : void
		{
			
			var rect:Rectangle = new Rectangle();
			rect.width = _bitmapData.rect.width;
			rect.height = _bitmapData.rect.height;
			if( isGhost == false ) {
				rects.push(rect);
				updateBitmapData(rect,_bitmapData, isGhost);
			}
			else {
				rectsGhost.push(rect);
				updateBitmapData(rect,_bitmapData, isGhost);
			}
						
			
		}
		public function updateBitmapData(rect:Rectangle ,_bitmapData:BitmapData, isGhost:Boolean):void {
			var sorted:Array = new Array();
     		var o:Rectangle;
     		if( isGhost ) {
	     		for(var i:int=0 ; i < this.rectsGhost.length ; i++){
	     			o = rectsGhost[i] as Rectangle;
	     			sorted.push(o);
	     		}
     		} else {
     			for(var i:int=0 ; i < this.rects.length ; i++){
	     			o = rects[i] as Rectangle;
	     			sorted.push(o);
	     			trace("o="+o);
	     		}
     		}
     		sorted.sort(triRectanglesByHeigth);
     		var x:int = 0;
     		var y:int = 0;
     		var maxw:int = 0;
     		var maxh:int = 0;
     		for(var i:int=0 ; i < sorted.length ; i++ ){
     			o = sorted[i] as Rectangle;
     			x+= o.width;
     			if( x >= 2880) {
     				x = 0;
     				y += o.height;         				
     			} else {
     				if( maxw < x ) maxw = x ;
     				if( maxh < (y + o.height)) maxh = y + o.height;
     			}        			         		
     		}
     		
     		var newBigBd:BitmapData = new BitmapData(maxw, maxh,true,0xffffffff);     		         		
     		x = 0;
     		y = 0;
     		trace(rect);
			for(var i:int=0 ; i < sorted.length ; i++ ){
     			o = sorted[i] as Rectangle;
     			if( !rect || rect != o ) {
     				if( isGhost ) {
     					newBigBd.copyPixels(this.bitmapDataGhost, o, new Point(x,y));
     				} else {
     					newBigBd.copyPixels(this.bitmapData, o, new Point(x,y));
     				}
     				
     			} else {
     				trace("rect==o");
     				newBigBd.copyPixels(_bitmapData, _bitmapData.rect, new Point(x,y));	         				
     			}
     			o.x = x;
     			o.y = y; 
     			x+= o.width;
     			if( x >= 2880) {
     				x = 0;
     				y += o.height;         				
     			}     			
			} 
			if( isGhost ) {
				this.bitmapDataGhost = newBigBd;
			} else {
				this.bitmapData = newBigBd;
			}
		}
		public function triRectanglesByHeigth(g1:Rectangle, g2:Rectangle):int {
         	if( g1.height < g2.height) return -1;
         	else return 1;
         }
	}
}